package com.langangkj.tools.redis;

import com.langangkj.tools.redis.handler.meta.impl.SimpleRedisKeyMeta;
import com.langangkj.tools.redis.handler.provider.RedisSyncKeyProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Set;

/**
 * @author ZhouChuGang
 * @version 1.0
 * @project redis-sync-tools
 * @date 2020/4/22 14:33
 * @Description redis同步工具启动器
 */
@Slf4j
@SpringBootApplication
public class RedisSyncApplication implements CommandLineRunner {
    public static void main(String[] args) {
        SpringApplication.run(RedisSyncApplication.class, args);
    }

    @Autowired
    @Qualifier("srcRedisTemplate")
    private RedisTemplate<byte[], byte[]> srcRedisTemplate;

    @Autowired
    private RedisSyncKeyProvider redisSyncKeyProvider;

    @Override
    public void run(String... args) throws Exception {
        Set keys = srcRedisTemplate.keys("*".getBytes());
        log.info("" + keys.size());
        keys.parallelStream().forEach(key -> {
            try {
                DataType type = srcRedisTemplate.type((byte[]) key);
                log.info("写入列队: {} --- {} ", key, type);
                SimpleRedisKeyMeta keyMeta = new SimpleRedisKeyMeta();
                keyMeta.setDataType(type);
                keyMeta.setKeyName(key);
                redisSyncKeyProvider.getBlockingQueue().put(keyMeta);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }
}
